﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Microsoft.Win32;
using Excel = Microsoft.Office.Interop.Excel;

namespace QAToolsAddin {
    public class Commands : Object {
        private readonly Excel.Application _excelapp;
        private System.Globalization.CultureInfo _culture;

        public Commands(Excel.Application application) {
            _excelapp = application;
            _culture = Utils.GetSystemCulture();
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
            System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += (sender, ex) => new GenericDialogs.FrmException(_excelapp, (Exception)ex.ExceptionObject).ShowDialog();
            ((Action<object>)delegate {
                new Components.ExcelForm(null);
                new CustomDialogs.FrmAbout(null, null);
                new CustomDialogs.FrmCombine(null, null);
                new CustomDialogs.FrmCompare(null, null);
                new CustomDialogs.FrmDistinct(null, null);
                new CustomDialogs.FrmImport(null, null);
                new GenericDialogs.InputBox(null);
            }).BeginInvoke(null, null, null);
        }

        private readonly Dictionary<string, bool> _ctrlEnabled = new Dictionary<string, bool>{
            {"btUndo_Click", false}
        };

        public bool GetControlEnabled(string control_name) {
            if (_ctrlEnabled.ContainsKey(control_name))
                return _ctrlEnabled[control_name];
            else
                return true;
        }

        public void SetControlEnabled(string control_name, bool enable) {
            if (!_ctrlEnabled.ContainsKey(control_name))
                _ctrlEnabled.Add(control_name, enable);
            else
                _ctrlEnabled[control_name] = enable;
        }

        public void InvokeAction(string name) {
            try {
                int ptindex = name.LastIndexOf('.');
                if (ptindex > 0) {
                    var type = "QAToolsAddin." + name.Substring(0, ptindex);
                    var methode = name.Substring(ptindex + 1);
                    var classType = Assembly.GetExecutingAssembly().GetType(type);
                    if (classType == null) throw new Exception("Missing type : " + type);
                    var constructor = classType.GetConstructor(new Type[] { typeof(Excel.Application), typeof(CultureInfo) });
                    if (constructor == null) throw new Exception("Missing constructor : " + type);
                    var instance = constructor.Invoke(new object[] { _excelapp, _culture });
                    var methodeInfo = classType.GetMethod(methode, new Type[0]);
                    if (methodeInfo == null) throw new Exception("Missing methode : " + methode);
                    methodeInfo.Invoke(instance, null);
                } else {
                    var methodeInfo = typeof(Commands).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
                    if (methodeInfo == null) throw new Exception("Missing methode : " + name);
                    methodeInfo.Invoke(this, null);
                }
            } catch (Exception ex) { new QAToolsAddin.GenericDialogs.FrmException(_excelapp, ex).ShowDialog(); }
        }

        private void btUndo_Click() {
            Undo.UndoLastArea();
        }

        private void btHelp_Click() {

        }

        private void btUninstall_Click() {
            new System.Threading.Thread((System.Threading.ThreadStart)delegate {
                string path = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\QAToolsAddin_is1", "UnInstallString", null);
                if (string.IsNullOrEmpty(path))
                    path = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QAToolsAddin_is1", "UnInstallString", null);
                if (!string.IsNullOrEmpty(path))
                    System.Diagnostics.Process.Start(path);
                //System.Diagnostics.Process.Start("msiexec", "/x {E843173D-D872-485A-8BA9-9300A602B2A8}");
            }).Start();
        }

    }
}
